<template>
  <list-card>
    <el-form ref="queryForm" slot="search" :inline="true" :model="queryParams" size="small">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入角色名称" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="角色编码" prop="code">
        <el-input v-model="queryParams.code" clearable placeholder="请输入角色编码" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="角色状态">
          <el-option v-for="item in localDict.role_status" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operate">
      <el-button v-has-permi="['system:role:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['system:role:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <el-button v-has-permi="['system:role:remove']" icon="el-icon-data-board" plain size="mini" @click="handleRecycle">
        回收站
      </el-button>
      <el-button v-has-permi="['system:role:export']" icon="el-icon-download" plain size="mini" type="warning"
                 @click="handleExport">导出
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <el-table slot="content" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column label="角色编号" prop="code" width="120"/>
      <el-table-column :show-overflow-tooltip="true" label="角色名称" prop="name" width="150"/>
      <el-table-column label="角色类型" prop="type" width="150">
        <template slot-scope="scope">
          <dict-tag :options="localDict.role_type" :value="scope.row.type" show-label/>
        </template>
      </el-table-column>
      <el-table-column label="权限范围" prop="scope" width="150">
        <template slot-scope="scope">
          <dict-tag :options="localDict.role_scope" :value="scope.row.scope" show-label/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="localDict.role_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" prop="sort" width="100"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template v-if="scope.row.roleId !== 1" slot-scope="scope">
          <el-button v-has-permi="['system:role:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-has-permi="['system:role:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
          <el-dropdown v-has-permi="['system:role:edit']" size="mini"
                       @command="(command) => handleCommand(command, scope.row)">
            <el-button icon="el-icon-d-arrow-right" size="mini" type="text">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="auth" icon="el-icon-user">分配用户</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.scope === '99'" command="scope" icon="el-icon-circle-check">数据权限</el-dropdown-item>
              <el-dropdown-item command="permission" icon="el-icon-circle-check">菜单权限</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="pageParam.total>0" slot="page" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                :total="pageParam.total" @pagination="getList"/>
    <auth-card ref="auth" @ok="getList"/>
    <role-model ref="model" @ok="getList"/>
    <scope-card ref="scope" @ok="getList"/>
    <permission-card ref="permission" @ok="getList"/>
    <recycle-card ref="recycle" :base-url="baseUrl" :columns="columns" :dicts="localDict" @ok="getList"/>
    <!--    <el-drawer :title="drawer.title" :visible.sync="drawer.open" append-to-body class="drawer-container" size="800px">-->
    <!--    </el-drawer>-->
  </list-card>
</template>

<script>
import {getStaticData} from "@/api/common";
import {list, remove} from "@/api/base";
import ListCard from "@/components/Card/ListCard.vue";
import RoleModel from "@/views/system/role/components/ModelCard.vue";
import AuthCard from "@/views/system/role/components/AuthCard.vue";
import ScopeCard from "@/views/system/role/components/ScopeCard.vue";
import PermissionCard from "@/views/system/role/components/PermissionCard.vue";
import RecycleCard from "@/components/Card/RecycleCard.vue";

export default {
  name: "Role",
  components: {PermissionCard, ScopeCard, RecycleCard, AuthCard, RoleModel, ListCard},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      baseUrl: '/system/role/',
      // 显示搜索条件
      showSearch: true,
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        name: undefined,
        code: undefined,
        status: undefined
      },
      columns: [
        {label: '角色编号', props: 'code', dict: false, visible: true, width: 100},
        {label: '角色名称', props: 'name', dict: false, visible: true, width: 100},
        {label: '角色类型', props: 'type', dict: true, code: 'role_type', tag: true, visible: true, width: 140},
        {label: '数据权限', props: 'scope', dict: true, code: 'role_scope', tag: true, visible: true, width: 140},
      ],
      localDict: {
        role_type: [],
        role_scope: [],
        role_status: [],
      },

    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery();
  },
  methods: {
    async getLocalDict() {
      await getStaticData('system-role-type').then(res => {
        this.localDict.role_type = res.data
      })
      await getStaticData('system-role-scope').then(res => {
        this.localDict.role_scope = res.data
      })
      await getStaticData('system-status').then(res => {
        this.localDict.role_status = res.data
      })
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      this.$refs[command].visible = true
      this.$refs[command].roleId = row.id
      this.$refs[command].resetQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的角色？').then(() => {
        remove(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        })
      });
    },
    handleRecycle() {
      this.$refs.recycle.visible = true
      this.$refs.recycle.title = "角色管理回收站"
      this.$refs.recycle.resetQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

